import java.sql.*;

public class Creator {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// Creates and populates a database
		Class.forName("org.sqlite.JDBC");
		// Given path indicates table created one level above project folder (repo root)
		Connection conn = DriverManager.getConnection("jdbc:sqlite:../HyPeerWeb.db");
	    Statement stat = conn.createStatement();
	    createNodeTable(stat);
	    createNeighborTable(stat);
	    createSurrogateNeighborTable(stat);
	    conn.close();
	    System.out.println("HyPeerWeb.db created");
	}
	
	private static void createNodeTable(Statement stat) throws SQLException {
	    stat.executeUpdate("drop table if exists Node;");
	    stat.executeUpdate("create table Node(" +
	    		"WebId INT PRIMARY KEY," +
	    		"State INT," +
	    		"Height INT," +
	    		"Fold INT," +
	    		"SurrogateFold INT);");
	}
	
	private static void createNeighborTable(Statement stat) throws SQLException {
	    // Create neighbor table
	    stat.executeUpdate("drop table if exists Neighbor;");
	    stat.executeUpdate("create table Neighbor(" +
	    		"Node1 INT," +
	    		"Node2 INT," +
	    		"FOREIGN KEY(Node1) REFERENCES Node(WebId)," +
	    		"FOREIGN KEY(Node2) REFERENCES Node(WebId));");
	}

	private static void createSurrogateNeighborTable(Statement stat) throws SQLException {
	    // Create neighbor table
	    stat.executeUpdate("drop table if exists SurrogateNeighbor;");
	    stat.executeUpdate("create table SurrogateNeighbor(" +
	    		"Node1 INT," +
	    		"Node2 INT," +
	    		"FOREIGN KEY(Node1) REFERENCES Node(WebId)," +
	    		"FOREIGN KEY(Node2) REFERENCES Node(WebId));");
	}
}
